package com.hxhk.service.impl.module.cgform.config;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.hxhk.dao.module.cgform.config.CgFormHeadMapper;
import com.hxhk.pojo.Criteria;
import com.hxhk.pojo.module.cgform.config.CgFormHeadEntity;
import com.hxhk.pojo.module.staff.Staff;
import com.hxhk.service.module.cgform.config.CgFormHeadServiceI;

@Service
public class CgFormHeadServiceImpl implements CgFormHeadServiceI{
	private static final Logger logger = LoggerFactory
			.getLogger(CgFormHeadServiceImpl.class);
	@Autowired
	private CgFormHeadMapper cgformHeadMapper;
	
	@Override
	public int countByExample(Criteria example) {
		int count = this.cgformHeadMapper.countByExample(example);
		logger.debug("count: {}", count);
		return count;
	}

	@Override
	public CgFormHeadEntity selectByPrimaryKey(String id) {
		return this.cgformHeadMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<CgFormHeadEntity> selectByExample(Criteria example) {
		return this.cgformHeadMapper.selectByExample(example);
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public String saveCgFormHeadEntity(Criteria example) {
		CgFormHeadEntity cgformHead = (CgFormHeadEntity) example.get("staff");
		int result = 0;
		if (StringUtils.isBlank(cgformHead.getId())) {
			// 新建的员工
			result = this.cgformHeadMapper.insertSelective(cgformHead);
		} else {
			// 修改的用户
			result = this.cgformHeadMapper.updateByPrimaryKeySelective(cgformHead);
		}
		if (result == 0) {
			return "00";
		}
		return "01";
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public String deleteByPrimaryKey(Criteria example) {
		int result = 0;
		String id = example.getAsString("id");
		result = this.cgformHeadMapper.deleteByPrimaryKey(id);
		return result > 0 ? "01" : "00";
	}

}
